import time

from pycparser.ply.yacc import token
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

# 1.创建浏览器驱动对象
driver=webdriver.Chrome()
driver.maximize_window()
# 2.通过get方法获取网页
driver.get("http://localhost:82/")
driver.find_element(By.PARTIAL_LINK_TEXT,"登录").click()
driver.find_element(By.ID,"username").send_keys("13800138006")
driver.find_element(By.ID,"password").send_keys("123456")
driver.find_element(By.ID,"verify_code").send_keys("8888")
driver.find_element(By.CLASS_NAME,"J-login-submit").click()
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"a[href='/index.php/Home/Index/index.html']").click()
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"#q").send_keys("小米")
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"button[type='submit']").click()
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"img[data-original='/public/upload/goods/thumb/5/goods_thumb_5_0_236_236.png']").click()
time.sleep(1)
action=ActionChains(driver)
ele=driver.find_element(By.CSS_SELECTOR,"#number")
ele.send_keys(Keys.CONTROL, 'a')
time.sleep(1)
ele.send_keys(Keys.DELETE)
time.sleep(1)
ele.send_keys("2")
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"#buy_now").click()
time.sleep(1)
alert=driver.switch_to.alert
time.sleep(1)
alert.accept()
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"#addNewAddress").click()
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"input[name='consignee'][type='text']").send_keys("cus")
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"input[name='consignee'][type='text']").send_keys(time.strftime("%Y%m%d%H%M%S"))
time.sleep(1)
driver.find_element(By.CSS_SELECTOR,"input[placeholder='固话号码格式:xx-xx(例如: 0755-86140485)']").send_keys("17630135209")
time.sleep(0.5)
Select(driver.find_element(By.CSS_SELECTOR,"#province")).select_by_visible_text("河南省")
time.sleep(0.5)
Select(driver.find_element(By.CSS_SELECTOR,"#city")).select_by_visible_text("周口市")
time.sleep(0.5)
Select(driver.find_element(By.CSS_SELECTOR,"#district")).select_by_visible_text("川汇区")
time.sleep(0.5)
Select(driver.find_element(By.CSS_SELECTOR,"#twon")).select_by_visible_text("开发区")
time.sleep(0.5)
driver.find_element(By.CSS_SELECTOR,"input[placeholder='详细地址']").send_keys("周口艾腾教育")
time.sleep(0.5)
driver.find_element(By.CSS_SELECTOR,"input[name='zipcode']").send_keys("466000")
time.sleep(0.5)
driver.find_element(By.CSS_SELECTOR,"#address_submit").click()
time.sleep(0.5)
driver.find_element(By.CSS_SELECTOR,"div[class='addr-switch'] span").click()

# 查看地址最后一个并点击
time.sleep(1)
# last-child 最后一个孩子  :first-child 第一个孩子 nth-child(数目)
driver.find_element(By.CSS_SELECTOR, '[id="ajax_address"] ul li:last-child div:first-child').click()
driver.find_element(By.CSS_SELECTOR,"textarea[placeholder='最多输入50个字']").send_keys("需要上门安装,联系电话:17630136209")
time.sleep(1)
js=("window.scrollTo(0, 1000)")
driver.execute_script(js)
time.sleep(1)
# 获取账户余额
balance = float(driver.find_element(By.XPATH, "/html/body/div[11]/div/div[2]/div[3]/p[2]/label/span").text)
time.sleep(1)
# 获取应付总额
payables_element = driver.find_element(By.CSS_SELECTOR, "#payables")
time.sleep(1)
payables_text = payables_element.text.strip()
time.sleep(1)
payables_clean = payables_text.replace('￥', '').replace(',', '')
time.sleep(1)
payables = float(payables_clean)
driver.find_element(By.CSS_SELECTOR, "#user_money_checkbox").click()
# 获取账户余额输入框
balance_input = driver.find_element(By.CSS_SELECTOR, "#user_money")
time.sleep(1)
# 比较
# 处理支付金额
if balance == 0:
    driver.find_element(By.CSS_SELECTOR, "#submit_order").click()
    time.sleep(3)
else:
    if balance >= payables:
        balance_input.send_keys(str(payables))
    else:
        balance_input.send_keys(str(balance))
    time.sleep(1)
    driver.find_element(By.CSS_SELECTOR, "#pay_pwd").send_keys("123456")
    driver.find_element(By.CSS_SELECTOR, "#submit_order").click()
# 尝试获取订单号（先试余额不足场景，再试余额充足场景）
try:
    # 先尝试余额不足的元素定位
    order_number_text = driver.find_element(By.CSS_SELECTOR, ".succ-p").text
    index = order_number_text.find("订单号：")
    if index != -1:
        order_number = order_number_text[index + len("订单号："):].strip().split()[0]
        print("订单号：", order_number)
    else:
        print("无订单号")
except:
    # 余额充足场景的处理
    time.sleep(3)
    order_number_value = driver.find_element(By.CSS_SELECTOR, "div.home-main span:nth-child(2)").text
    print(order_number_value)
# 4.关闭浏览器对象
driver.quit()


